package com.bawei.detail

import android.graphics.Color
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.MovieEntity
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.detail.adapter.DetailPager2Adapter
import com.bawei.detail.databinding.ActivityMovieDetailBinding
import com.bawei.detail.entity.TabEntity
import com.bawei.detail.viewmodel.DetailViewModel
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import master.flame.danmaku.controller.DrawHandler
import master.flame.danmaku.danmaku.model.BaseDanmaku
import master.flame.danmaku.danmaku.model.DanmakuTimer
import master.flame.danmaku.danmaku.model.IDanmakus
import master.flame.danmaku.danmaku.model.android.DanmakuContext
import master.flame.danmaku.danmaku.model.android.Danmakus
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser

@AndroidEntryPoint
@Route(path = "/detail/activity")
class MovieDetailActivity : BaseMviActivity<ActivityMovieDetailBinding>() {


    private val mViewModel: DetailViewModel by viewModels()

    @Autowired
    lateinit var movieEntity: MovieEntity

    private var tabData = arrayListOf<CustomTabEntity>()
    private var mFragments = arrayListOf<Fragment>()
    private lateinit var mPagerAdapter: DetailPager2Adapter
    private var mMoviePlayer: StandardGSYVideoPlayer? = null
    private lateinit var mDanMuContext : DanmakuContext

    override fun getLayoutId(): Int = R.layout.activity_movie_detail

    override fun initViews() {
        ARouter.getInstance().inject(this)
        //初始化GSY
        mMoviePlayer = mDataBinding.moviePlayer
        mMoviePlayer?.setUpLazy(movieEntity.videopath, true, null, null, "")
        mMoviePlayer?.backButton?.visibility = View.GONE

        //点击弹幕按钮
        mDataBinding.tvDanmu.setOnClickListener {
            mDataBinding.llDanmuLayout.visibility = View.VISIBLE
        }

        mDataBinding.ivDanmuSend.setOnClickListener {
            var msg = mDataBinding.etDanmuInput.editableText.toString().trim()
            if(!msg.isNullOrEmpty()){
                sendDanMu(msg)
            }
        }

        //初始化Tab
        tabData.add(TabEntity("简介"))
        tabData.add(TabEntity("评论(${movieEntity.favoritecount})"))
        mDataBinding.tabLayout.setTabData(tabData)

        //初始化ViewPager2
        mFragments.add(IntroduceFragment.newInstance(movieEntity.userid,movieEntity.videotypeid))
        mFragments.add(CommendFragment.newInstance("7040684506063307271"))
        mPagerAdapter = DetailPager2Adapter(mFragments, this@MovieDetailActivity)
        mDataBinding.vp2.adapter = mPagerAdapter

        //ViewPager2和CommonTabLayout绑定
        mDataBinding.tabLayout.setOnTabSelectListener(object: OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                mDataBinding.vp2.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        mDataBinding.vp2.registerOnPageChangeCallback(object: OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                mDataBinding.tabLayout.currentTab = position
            }
        })

        //初始化弹幕
        initDanMu()
    }

    private fun initDanMu(){
        //清理存在的弹幕
        mDataBinding.danmuView.enableDanmakuDrawingCache(true)
        //创建弹幕的上下文
        mDanMuContext = DanmakuContext.create()
        //准备弹幕
        mDataBinding.danmuView.prepare(object: BaseDanmakuParser(){
            override fun parse(): IDanmakus {
                return Danmakus()
            }
        }, mDanMuContext)
        //设置回调
        mDataBinding.danmuView.setCallback(object: DrawHandler.Callback{
            override fun prepared() {
                lifecycleScope.launch {
                    //mViewModel.channel.send()
                }
            }

            override fun updateTimer(timer: DanmakuTimer?) {

            }

            override fun danmakuShown(danmaku: BaseDanmaku?) {

            }

            override fun drawingFinished() {

            }
        })
    }

    private fun sendDanMu(danmuStr: String){
        //清理弹幕
        mDataBinding.danmuView.clear()
        mDataBinding.danmuView.removeAllDanmakus(false)
        //创建弹幕实体
        var danmuEntity = mDanMuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL)
        danmuEntity.text = danmuStr
        danmuEntity.textSize = 30f
        danmuEntity.textColor = Color.YELLOW
        danmuEntity.padding = 10
        danmuEntity.time = 800
        //添加到弹幕容器中
        mDataBinding.danmuView.addDanmaku(danmuEntity)
        mDataBinding.danmuView.start()

        //清空输入框
        mDataBinding.danmuView.postDelayed({
            mDataBinding.etDanmuInput.setText("")
            mDataBinding.llDanmuLayout.visibility = View.GONE
        }, 300)
    }

    override fun onStart() {
        super.onStart()
        if(mMoviePlayer != null){
            mMoviePlayer?.startPlayLogic()
        }
    }

    override fun onResume() {
        super.onResume()
        if(mMoviePlayer != null){
            mMoviePlayer?.onVideoResume()
        }
    }

    override fun onPause() {
        super.onPause()
        if(mMoviePlayer != null){
            mMoviePlayer?.onVideoPause()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(mMoviePlayer != null){
            mMoviePlayer?.release()
            mMoviePlayer = null
        }
    }





}